package sjtu.ist.warehouse4ws.model.file;

import java.util.ArrayList;


import sjtu.ist.warehouse4ws.jcr.RepositoryMoudl;


public class CompanyFileManagerImplement implements CompanyFileManager{
	ArrayList<Suit> suits=null;
	String companyName;
	RepositoryMoudl rm;

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	private static ArrayList<CompanyFileManager> singleCompany=new ArrayList<CompanyFileManager>();
	
	private CompanyFileManagerImplement(String companyName,RepositoryMoudl rm)
	{
		this.companyName=companyName;
		this.rm=rm;
	}
	
	public synchronized static CompanyFileManager getInstance(String companyName,RepositoryMoudl rm)
	{
		for(CompanyFileManager company:singleCompany)if(company.getName().endsWith(companyName))return company;
		CompanyFileManager temp=new CompanyFileManagerImplement(companyName,rm);
		singleCompany.add(temp);
		return temp;
	}
	private void suitsInit(){
		suits = new ArrayList<Suit>();
		ArrayList<String> names = rm.getNodesNames(companyName);
		ArrayList<String> pathes = rm.getNodesPathes(companyName);
		for (int i = 0; i < names.size(); i++) {
			
			suits.add(new SuitImplement(names.get(i), pathes.get(i)));
		}
	}
	public ArrayList<Suit> getSuits() {
		if (suits == null) {
			suitsInit();
		};
		return suits;
	}

	public void setSuits(ArrayList<Suit> suits) {
		this.suits = suits;
	}

	public Suit getSuit(String name)
	{
		if(suits!=null)suitsInit();
		for(int i=0;i<suits.size();i++)
		{
			if(suits.get(i).getName().equals(name))return suits.get(i);
		}
		return null;
	}

	@Override
	public boolean makeCompanyFolder(String companyId) {
		// TODO Auto-generated method stub
		this.companyName=companyId;
		rm.createWorkSpace(companyId);
		return true;
	}
	
	@Override
	public boolean makeCopanyFolder(){
		rm.createWorkSpace(companyName);
		return true;
	}

	@Override
	public String getName() {
		return companyName;
	}
	

}
